package com.fsm.system.serviceImpl;

import com.alibaba.excel.EasyExcel;
import com.fsm.common.tools.Result.Result;
import com.fsm.system.entity.excelbo.SystemRoleBo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.fsm.system.entity.TSystemRole;
import com.fsm.system.mapper.TSystemRoleMapper;
import com.fsm.system.service.TSystemRoleService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 *  角色管理服务层实现。
 *
 * @author
 * @since
 */
@Service
public class TSystemRoleServiceImpl extends ServiceImpl<TSystemRoleMapper, TSystemRole> implements TSystemRoleService {

    /**
     * 公共整合查询条件
     * @param tSystemRole
     * @return
     */
    private static QueryWrapper getSelectQueryWrapper(TSystemRole tSystemRole){
        QueryWrapper queryWrapper = new QueryWrapper();
        return queryWrapper;
    }

    /**
     * 添加角色管理
     *
     * @param tSystemRole
     * @return
     */
    @Override
    @Transactional
    public Result addTSystemRole(TSystemRole tSystemRole) throws Exception {
        int sign = this.getMapper().insert(tSystemRole);
        if(sign == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.fail("添加角色管理失败");
        }
        return Result.ok();
    }

    /**
     * 删除角色管理
     *
     * @param tSystemRole
     * @return
     */
    @Transactional
    @Override
    public Result deleteTSystemRole(TSystemRole tSystemRole) throws Exception {
        int sign = this.getMapper().deleteById(tSystemRole.getId());
        if(sign == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.fail("删除角色管理失败");
        }
        return Result.ok();
    }

    /**
     * 更新角色管理
     *
     * @param tSystemRole
     * @return
     */
    @Transactional
    @Override
    public Result updateTSystemRole(TSystemRole tSystemRole) throws Exception {
        int sign = this.getMapper().update(tSystemRole);
        if(sign == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.fail("更新角色管理失败");
        }
        return Result.ok();
    }

    /**
     * 分页查询角色管理
     *
     * @param tSystemRole
     * @return
     */
    @Override
    public Result pageTSystemRole(TSystemRole tSystemRole) throws Exception {
        QueryWrapper queryWrapper = getSelectQueryWrapper(tSystemRole);
        Page<TSystemRole> tSystemRolePage = this.getMapper().paginateWithRelations(tSystemRole.getPage(),tSystemRole.getLimit(),queryWrapper);
        return Result.ok(tSystemRolePage.getRecords(),tSystemRolePage.getTotalRow());
    }

    /**
     * 查询角色管理
     *
     * @param tSystemRole
     * @return
     */
    @Override
    public Result getTSystemRoleList(TSystemRole tSystemRole) throws Exception{
        QueryWrapper queryWrapper = getSelectQueryWrapper(tSystemRole);
        List<TSystemRole> tSystemRoleList = this.getMapper().selectListWithRelationsByQuery(queryWrapper);
        return Result.ok(tSystemRoleList,tSystemRoleList.size());
    }

    /**
     * 通过ID查询角色管理
     *
     * @param tSystemRole
     * @return
     */
    @Override
    public Result getTSystemRoleById(TSystemRole tSystemRole) throws Exception{
        TSystemRole tSystemRoleOne = this.getMapper().selectOneWithRelationsById(tSystemRole.getId());
        return Result.ok(tSystemRoleOne);
    }
    /**
     * 导出角色信息
     *
     * @param
     * @return 分页对象
     */
    @Override
    public Result uploadRole(TSystemRole tSystemRole, HttpServletResponse response) throws Exception {
        //准备需要输出的数据 调用业务层，获得所有需要导出的数据
        QueryWrapper queryWrapper = getSelectQueryWrapper(tSystemRole);
        List<TSystemRole> tSystemRoleList = this.getMapper().selectListWithRelationsByQuery(queryWrapper);
        List<SystemRoleBo> systemRoleBos = new ArrayList<>();
        for (TSystemRole tSystemRoleOne : tSystemRoleList){
            SystemRoleBo systemRoleBo = new SystemRoleBo();
            BeanUtils.copyProperties(tSystemRoleOne,systemRoleBo);
            systemRoleBos.add(systemRoleBo);
        }
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("角色", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        OutputStream outputStream = response.getOutputStream();
        EasyExcel.write(outputStream, SystemRoleBo.class).sheet("角色数据").doWrite(systemRoleBos);
        return Result.ok();
    }
}
